#include "heartRequest.h"
size_t heartCallback(void *contents, size_t size, size_t nmemb, void *userp) 
{
    size_t realsize = size *nmemb;
    char * result = (char*)contents;
    printf("%s\n",result);
    cJSON *jsonstr = cJSON_Parse(result);
    if(jsonstr != NULL){
        cJSON *successRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"success");
        cJSON *messageRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"message");
        cJSON *codeRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"code");
        if(cJSON_IsTrue(successRoot)){
            cJSON *dataRoot = cJSON_GetObjectItemCaseSensitive(jsonstr,"data");
            cJSON *adList = cJSON_GetObjectItemCaseSensitive(dataRoot,"ad_list");
            cJSON *cmdList = cJSON_GetObjectItemCaseSensitive(dataRoot,"cmd_list");
            parseCmd(cmdList);
            handleAdList(adList);
            
        }else{
            perror("网络异常\n");
        }
    }
    return realsize;
}
void sendHeartRequest(char *ip,char *sn){
    char *url = "http://%s/hearts.json?sn=%s";
    char *realUrl = calloc(1,100);
    sprintf(realUrl,url,ip,sn);
    printf("request address:%s\n",realUrl);
    sendGetRequest(realUrl,heartCallback);
}